** National Election Studies
cd "/Users/mmoral/Dropbox/Sabancı University/-ONGOING/On the Relationship between Party Polarization and Citizen Polarization/NES"

** National Election Studies
* Australia 1998
use "Australia/aes_1998_01001.dta", clear
rename (b11reps b10own) (lastvote resleft)
rename (b10lib b10alp b10nat b10dem b10onp b10grn) left(#), addnumber
recode *left* (-1=.a)
recode lastvote (1/7=1) (else=0), gen(vote)
recode lastvote (-1=.a) (5=6) (6=5) (7/9=.a)

replace left5=.a	/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left6=.a	/* for the political knowledge scale */

forval i=1/6 {
capture noisily {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode h2 (-1=.a) (0/1=0) (2/9=1), gen(educsca)

sum left2 left1 	/* ALP first, LPA second */
gen ideoknow=0
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=199810
gen iso=1036
reshape long left, i(iso) j(party)
saveold "Australia (1998) v1.dta", replace

* Australia 2001
use "Australia/aes_2001_01052-c.dta", clear
drop if sample==1 /* Online survey w/ different questions */
rename leftscho alscho
rename (b11reps b10own) (lastvote resleft)
rename (b10lib b10alp b10nat b10dem b10onp b10grn) left(#), addnumber
recode *left* (-1=.a)
recode lastvote (1/7=1) (else=0), gen(vote)
recode lastvote (-1=.a) (5=6) (6=5) (7/8=.a)

replace left5=.a	/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left6=.a	/* for the political knowledge scale */

forval i=1/6 {
capture noisily {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode h2 (-1=.a) (0/3=0) (4/9=1), gen(educsca)

sum left2 left1 	/* ALP first, LPA second */
gen ideoknow=0
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=200111
gen iso=1036
reshape long left, i(iso) j(party)
saveold "Australia (2001) v1.dta", replace

* Australia 2010
use "Australia/aes_2010_01228.dta", clear
rename (b9reps b8own) (lastvote resleft)
rename (b8lib b8alp b8nat b8grn) left(#), addnumber
recode *left* (-1=.a)
recode lastvote (1/5=1) (else=0), gen(vote)
recode lastvote (-1=.a) (5/6=.a)

forval i=1/4 {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode g1age (-1=.a) (10/16=0) (17/70=1), gen(educsca)

sum left2 left1 	/* ALP first, LPA second */
gen ideoknow=0
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=201008
gen iso=1036
reshape long left, i(iso) j(party)
saveold "Australia (2010) v1.dta", replace

* Australia 2016
use "Australia/aes_2016_01365.dta", clear
rename (B9_1 B8_1) (lastvote resleft)
rename B8_* left#, renumber
recode *left* (999=.a)
recode lastvote (1/97=1) (else=0), gen(vote)
recode lastvote (5/999=.a)

forval i=1/4 {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode G1_Age (996/999=.a) (15/16=0) (17/72=1), gen(educsca)

sum left2 left1 	/* ALP first, LPA second */
gen ideoknow=0
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=201607
gen iso=1036
reshape long left, i(iso) j(party)
saveold "Australia (2016) v1.dta", replace 

* Austria 2006
use "Austria/Austria 2006/ZA4888.dta", clear
rename (v6_2 v25) (lastvote resleft)
rename v26_*? left#, renumber
recode *left* (98/99=.a)
recode lastvote (1/7=1) (else=0), gen(vote)
recode lastvote (7/99=.a)

replace left6=.a				/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
								/* for the political knowledge scale */

foreach var of varlist *left*{ 	/* 1-11 scale converted to 0-10 */
replace `var'=`var'-1
}

forval i=1/6 {
capture noisily {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode v40 (9=.a) (1/4=0) (5/7=1), gen(educsca)

sum left2 left1 				/* SPÖ first, ÖVP second */
gen ideoknow=0
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=200610
gen iso=1040
reshape long left, i(iso) j(party)
saveold "Austria (2006) v1.dta", replace

* Denmark 2005
use "Denmark/Electionstudy-2005/Electionstudy-2005_F1.dta", clear
rename (v37 v252) (lastvote resleft) 
rename (v253 v254 v255 v256 v257 v258 v259) (left1 left2 left3 left4 left5 left6 left7)

recode *left* (88=.a)
recode lastvote (1/10=1) (82=1) (92=1) (else=0), gen(vote)
*recode lastvote (1=1) (2=2) (3=3) (4=.a) (5=4) (6=.a) (7=.a) (8=5) (9=6) (10=7) (else=.a) /* Codebook is wrong, see tabulation w/ v20 (vote in the previous election) */
*tab v20 lastvote
recode lastvote (1=1) (2=2) (3=3) (4=.a) (5=4) (6=.a) (7=5) (8=6) (9=.a) (10=7) (else=.a)

forval i=1/7 {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode v338 (9=.a) (1/3=0) (4/5=1), gen(educsca)

sum left6 left1 					/* V first, SD second */
gen ideoknow=0
replace ideoknow=1 if left6>left1 & left6!=.a & left1!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=200502
gen iso=1208
reshape long left, i(iso) j(party)
format *left* %9.6f
saveold "Denmark (2005) v1.dta", replace

* Denmark 2015
use "Denmark/Danish National Election Study_2015/data31083.dta", clear
rename (V51 V240) (lastvote resleft) 
rename (V241-V249) (left#), addnumber
keep if !missing(lastvote)

recode *left* (88=.a)
recode lastvote (1/12=1) (else=0), gen(vote)
recode lastvote (1=1) (2=2) (3=3) (4=4) (5=5) (6=.a) (7=6) (8=7) (9=8) (10=9) (else=.a)

forval i=1/9 {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)

gen education=1 if V351<88
replace education=2 if inlist(V351, 2, 3, 4, 9, 10)
replace education=3 if inlist(V351, 5, 6, 7, 8) | inlist(V353, 2, 3, 4, 5, 6)
replace education=4 if V353==1
replace education=5 if V353==7| V353==11
replace education=6 if V353==8| V353==9
replace education=7 if V353==10

recode education (1/4=0) (5/7=1), gen(educsca)

sum left1 left6					/* A first, O second */
gen ideoknow=0
replace ideoknow=1 if left1<left6 & left6!=.a & left1!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=201506
gen iso=1208
reshape long left, i(iso) j(party)
format *left* %9.6f
saveold "Denmark (2015) v1.dta", replace

* Finland 1991
use "Finland/FSD1016/Study/data/daF1016e.dta", clear
rename (q34_2 q4a) (lastvote resleft)
rename q4b_*? left#, renumber
recode *left* (0=.a) (missing=.a)
foreach var of varlist *left*{ 	/* 1-9 scale converted to 0-10 */
replace `var'=(10*`var'-10)/8
}
recode lastvote (1/12=1) (else=0), gen(vote)
recode lastvote (9/15=.a)

forval i=1/8 {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode q22 (4=.a) (1/2=0) (3=1), gen(educsca)

sum left3 left1 				/* SK first, SSDP second */
gen ideoknow=0
replace ideoknow=1 if left3>left1 & left3!=.a & left1!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=199103
gen iso=1246
reshape long left, i(iso) j(party)
saveold "Finland (1991) v1.dta", replace

* Greece 2012
use "Greece/100022-V6/elnes-2012-weighted.dta", clear
rename (Q1ELNES Q12) (lastvote resleft)
rename Q11* left#, renumber
recode *left* (97/98=.a)
recode Q5LH_a (1=1) (else=0), gen(vote)
recode lastvote (8/99=.a)

forval i=1/7 {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode D3 (96/97=.a) (1/6=0) (7/9=1), gen(educsca)

sum left1 left2 				/* ND first, Syriza second */
gen ideoknow=0
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=201205
gen iso=1300
reshape long left, i(iso) j(party)
saveold "Greece (2012) v1.dta", replace

* Netherlands 2017
use "Netherlands/1532695982605-Dutch_Parliamentary_Elect/DPES 2017 v1.3.dta", clear								/* Updated (07.06.19) */
rename (V163 V133) (lastvote resleft)
rename (V120-V132) (left#), addnumber
recode *left* (11=.a)
recode V160 (2=0), gen(vote)
recode lastvote (1=4) (2=2) (3=1) (4=8) (5=5) (6=6) (7=7) (8=9) (9=3) (10=10) (11=11) (12=12) (14=13) (else=.a)

forval i=1/13 {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode V368 (9=.a) (1/5=0) (6/8=1), gen(educsca)

sum left1 left3 				/* VVD first, PVV second */
gen ideoknow=0
replace ideoknow=1 if left1<left3 & left1!=.a & left2!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=201703
gen iso=1528
reshape long left, i(iso) j(party)
saveold "Netherlands (2017) v1.dta", replace

* New Zealand 1990
use "New Zealand/nzes90.dta", clear
drop left
rename (vot90e scalep) (lastvote resleft)
rename (scalelb scalent) left(#), addnumber
recode *left* (missing=.a)
recode nonvt (2=1) (1=0), gen(vote)
recode lastvote (0=.a) (3/8=.a)

foreach var of varlist *left*{ 	/* 1-7 scale converted to 0-10 */
replace `var'=(10*`var'-10)/6
}

forval i=1/2 {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode schyrs (10/16=0) (17/24=1), gen(educsca)

sum left2 left1 				/* National first, Labour second */
gen ideoknow=0
replace ideoknow=1 if left2>left1 & left2!=.a & left1!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=199010
gen iso=1554
reshape long left, i(iso) j(party)
saveold "New Zealand (1990) v1.dta", replace

* New Zealand 1993
use "New Zealand/nzes93.dta", clear
rename (vot93e nlscale) (lastvote resleft)
rename (lsclab lscnat lscall) left(#), addnumber
recode *left* (0=.a) (8=.a)
recode nvot (2=1) (1=0), gen(vote)
recode lastvote (0=.a) (4/7=.a)

foreach var of varlist *left*{ 	/* 1-7 scale converted to 0-10 */
replace `var'=(10*`var'-10)/6
}

forval i=1/3 {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode edold (11/17=0) (18/78=1), gen(educsca)

sum left2 left1 				/* National first, Labour second */
gen ideoknow=0
replace ideoknow=1 if left2>left1 & left2!=.a & left1!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=199311
gen iso=1554
reshape long left, i(iso) j(party)
saveold "New Zealand (1993) v1.dta", replace

* New Zealand 1999
use "New Zealand/nzes99.dta", clear
drop if scale==887 				/* Some respondents were not asked the l-r questions */
rename (nvot99p scale) (lastvote resleft)
rename (scnat sclab scnzf scall scact scgre) left(#), addnumber
recode *left* (99/887=.a)
recode lastvote (0=0) (else=1), gen(vote)
recode lastvote (0=.a) (6=.a) (1=2) (2=1) (3=4) (4=3) (7=6) (8/11=.a)

forval i=1/6 {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode sedqual (1/4=0) (5/6=1), gen(educsca)

sum left2 left1 				/* Labour first, National second */
gen ideoknow=0
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=199911
gen iso=1554
reshape long left, i(iso) j(party)
saveold "New Zealand (1999) v1.dta", replace

* New Zealand 2005
use "New Zealand/NZES_Release_05.dta", clear
rename (YVALID YNVT05P ylscale) (vote lastvote resleft)
rename (ynatscl ylabscl ynzscl yufscl yactscl ygrnscl yprgscl ymaoscl) left(#), addnumber
recode *left* (99=.a)
recode lastvote (0=.a) (1=2) (2=1) (3=6) (4=3) (5=5) (6=4) (7=8) (8=7) (9/13=.a)

replace left3=.a			/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left4=.a			/* for the political knowledge scale */
							
forval i=1/8 {
capture noisily {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode yeducate (1/4=0) (5/8=1), gen(educsca)

sum left2 left1 			/* Labour first, National second */
gen ideoknow=0
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=200509
gen iso=1554
reshape long left, i(iso) j(party)
saveold "New Zealand (2005) v1.dta", replace

* Spain 1989
use "Spain/1842/DA1842.dta", clear
rename (P10A P32) (lastvote resleft)
rename P33* left#, renumber
recode P10 (1=1) (else=0), gen(vote)
recode lastvote (0=.a) (1=1) (2=2) (3=3) (4=4) (5=14) (6=9) (7=17) (8=15) (9=16) (10=5) (11=6) (12=7) (13=8) (14/99=.a)
recode *left* (0=.a) (88/99=.a) 

replace left7=.a				/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left11=.a				/* for the political knowledge scale */
replace left12=.a
replace left13=.a
replace left15=.a
replace left16=.a
	
foreach var of varlist *left*{ 	/* 1-10 scale converted to 0-10 */
replace `var'=(10*`var'-10)/9
}

forval i=1/4 {					/* Only 4 most nationalized parties were asked to all respondents */
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode P37 (0=.a) (9=.a) (1/3=0) (4/8=1), gen(educsca)

sum left4 left3 				/* PSOE first, PP second */
gen ideoknow=0
replace ideoknow=1 if left4<left3 & left4!=.a & left3!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=198910
gen iso=1724
reshape long left, i(iso) j(party)
saveold "Spain (1989) v1.dta", replace

* Spain 2015
use "Spain/MD7715/DA7715.dta", clear
rename (PP31 PP35) (lastvote resleft)
rename PP36* left#, renumber
recode lastvote (1/96=1) (else=0), gen(vote)
recode lastvote (0=.a) (1=1) (2=2) (3=3) (4=4) (5=5) (6=7) (7=15) (8=8) (9=9) (10=18) (11=11) (12=12) (13=16) (14=14) (15=.a) (16=22) (17=17) (18=.a) (19=21) (20=6) (21=19) (22=13) (23=10) (24/99=.a)
recode *left* (98/99=.a)

foreach var of varlist *left*{ 	/* 1-10 scale converted to 0-10 */
replace `var'=(10*`var'-10)/9
}

replace left6=.a				/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left10=.a				/* for the political knowledge scale */
replace left13=.a
replace left14=.a
replace left17=.a
replace left19=.a
replace left20=.a
replace left21=.a
replace left22=.a

forval i=1/5 {					/* Only 5 most nationalized parties were asked to all respondents */
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode PP57A (98/99=.a) (0/11=0) (12/27=1), gen(educsca)

sum left1 left2 				/* PP first, PSOE second */
gen ideoknow=0
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=201512
gen iso=1724
reshape long left, i(iso) j(party)
saveold "Spain (2015) v1.dta", replace

* Spain 2016
use "Spain/MD3145/DA3145.dta", clear
rename (P31 P35) (lastvote resleft)
rename P36* left#, renumber
recode lastvote (1/96=1) (else=0), gen(vote)
recode lastvote (1=1) (2=2) (3=3) (4=4) (else=.a)
recode *left* (98/99=.a)

foreach var of varlist *left*{ 	/* 1-10 scale converted to 0-10 */
replace `var'=(10*`var'-10)/9
}

replace left5=.a				/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left11=.a				/* for the political knowledge scale */
replace left15=.a
replace left16=.a
											
forval i=1/4 {								/* Only 5 most nationalized parties were asked to all respondents, 4 are present in the CMP dataset */
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode ESTUDIOS (7/9=.a) (1/4=0) (5/6=1), gen(educsca)

sum left1 left2 				/* PP first, PSOE second */
gen ideoknow=0
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=201606
gen iso=1724
reshape long left, i(iso) j(party)
saveold "Spain (2016) v1.dta", replace

* Spain 2019 (November)
use "Spain/MD3269/3269.DTA", clear
rename (B22 C3) (lastvote resleft)
rename C4_* left#, renumber
recode lastvote (1/96=1) (else=0), gen(vote)
recode lastvote (2=1) (1=2) (18=3) (21=4) (4=5) (3=6) (50=7) (else=.a)
recode *left* (98/99=.a)

foreach var of varlist *left*{ 	/* 1-10 scale converted to 0-10 */
replace `var'=(10*`var'-10)/9
}

replace left3=.a				/* Non-legislative, minor parties w/ no vote share are coded as missing for the political knowledge scale */
replace left5=.a
replace left7=.a
replace left9=.a
replace left10=.a
replace left14=.a
replace left16=.a
replace left18=.a
replace left19=.a
replace left20=.a
											 
foreach i of numlist 1 2 4 6 {	/* Only 7 most nationalized parties were asked to all respondents */
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode estudios (7/9=.a) (1/4=0) (5/6=1), gen(educsca)

sum left1 left2 				/* PSOE first, PP second */
gen ideoknow=0
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=201911
gen iso=1724
reshape long left, i(iso) j(party)
saveold "Spain (2019) v1.dta", replace

* Switzerland 1995
use "Switzerland/dataset_64/64.dta", clear
rename q35 resleft
rename q34_* left#, renumber
recode q5 (2/3=1) (else=0), gen(vote)
recode vote (missing=0)
recode q11ent (4=1) (1=2) (9=3) (10=4) (15=5) (18=6) (19=7) (else=.a), gen(lastvote)
recode q12nt (1=2) (4=1) (9=3) (18=6) (19=7) (else=.a), gen(lastvote2)
replace lastvote=lastvote2 if missing(lastvote) & !missing(lastvote2)
recode *left* (-2/-1=.a) (11=.a) 

drop left8-left11 				/* Only 7 most nationalized parties were asked to all respondents */
forval i=1/7 { 
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode q103 (-1=.a) (9=.a) (1/3=0) (4/8=1), gen(educsca)

sum left7 left3 				/* SPS/PSS first, FDP/PRD second */
gen ideoknow=0
replace ideoknow=1 if left7<left3 & left7!=.a & left3!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=199510
gen iso=1756
reshape long left, i(iso) j(party)
saveold "Switzerland (1995) v1.dta", replace

* Switzerland 2015
use "Switzerland/dataset_726/726_Selects2015_PES_Data_v1.03.dta", clear
rename (f11800main7 f11100r f15200) (lastvote vote resleft)
rename f1513* left#, renumber
recode lastvote (1=4) (2=1) (3=6) (4=2) (5=6) (6=3) (7=5) (8=.a)
recode *left* (98/99=.a)

replace left8=.a				/* Non-legislative, minor parties w/ no vote share are coded as missing for the political knowledge scale */

foreach i of numlist 1/7 9 {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode educ_rr (1/5=0) (6/8=1), gen(educsca)

sum left4 left3 				/* SVP/UDC first, SPS/PSS second */
gen ideoknow=0
replace ideoknow=1 if left4>left3 & left4!=.a & left3!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=201510
gen iso=1756
reshape long left, i(iso) j(party)
saveold "Switzerland (2015) v1.dta", replace

* United Kingdom 1983
use "United Kingdom/BES 1983/83BES.dta", clear
rename (q9a q46g) (lastvote resleft)
rename (q46a q46b q46c q46d) left(#), addnumber 	/* Only 4 most nationalized parties were asked to all respondents */
recode q7a (2=0), gen(vote)
recode lastvote (1=1) (2=2) (4=3) (5=4) (else=.a)

recode *left* (95/99=.a) (97=10)

foreach var of varlist *left*{ 						/* -10/+10 scale converted to 0-10 */
replace `var'=10*(`var'+10)/20
}

forval i=1/4 { 
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode q59a (98/99=.a) (0/15=0) (16/22=1), gen(educsca)

sum left1 left2 									/* Conservatives first, Labour second */
gen ideoknow=0
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=198306
gen iso=1826
reshape long left, i(iso) j(party)
saveold "United Kingdom (1983) v1.dta", replace

* United Kingdom 2017
use "United Kingdom/BES 2017/bes_f2f_2017_v1.3.dta", clear
rename (b02 e01) (lastvote resleft)
rename q9* left(#), addnumber 
recode b01 (1=1) (else=0), gen(vote)
recode lastvote (1=2) (2=1) (3=3) (4=6) (5=7) (6=5) (7=4) (else=.a)

replace left4=.a				/* Non-legislative, minor parties w/ no vote share in the CMP data are coded */
replace left5=.a				/* as missing for the political knowledge scale */
replace left6=.a				/* Greens and Sinn Fein were only asked to Welsh and Scottish respondents */
								
drop if missing(left1)			/* Left-right placements were only asked to a subset of all respondents --coded as system missing  */
recode *left* (-2/-1=.a)

forval i=1/7 {
capture noisily {
egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
qui sum abserr`i'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i')
}
}

egen polknow=rowtotal(abserr*), missing
egen knowsca=cut(polknow), group(3)
egen knowsca_alt=cut(polknow), group(4)
recode edlevel (0/3=0) (4/5=1), gen(educsca)

sum left1 left2 									/* Conservatives first, Labour second */
gen ideoknow=0
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a

egen rescenter=mean(resleft)

egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

egen medianv=median(resleft)
egen resleftvot=sd(resleft) if vote==1
egen resleftabs=sd(resleft) if vote==0
egen resleftsop3=sd(resleft) if knowsca==2
egen resleftsop2=sd(resleft) if knowsca==1
egen resleftsop1=sd(resleft) if knowsca==0
egen resleftpkn=sd(resleft) if ideoknow==1
egen resleftpuk=sd(resleft) if ideoknow==0
egen reslefteducl=sd(resleft) if educsca==0
egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote
gen date=201706
gen iso=1826
reshape long left, i(iso) j(party)
saveold "United Kingdom (2017) v1.dta", replace

** ANES
* United States (1972-2016)
use "United States/anes_timeseries_cdf_dta_2/anes_timeseries_cdf.dta", clear
keep if mod(VCF0004, 4)==0 & VCF0004>=1972 			/* 2006 and 2010 elections are missing, party placements are not asked in 2002. Presidential election years are taken into account. */
rename (VCF0004 VCF0707 VCF0803 VCF0503 VCF0504) (year lastvote resleft left1 left2)
recode VCF0702 (2=1) (else=0), gen(vote)
recode lastvote (1=1) (2=2) (else=.a)
recode *left* (0=.a) (8/9=.a)

foreach var of varlist *left*{ 						/* 1-7 scale converted to 0-10 */
replace `var'=(10*`var'-10)/6
}

levelsof year, loc(levels)
forval i=1/2 { 
by year, sort: egen mleft`i'=mean(left`i')
by year, sort: gen abserr`i'=abs(left`i'-mleft`i')
foreach y of loc levels {
qui sum abserr`i' if year==`y'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i') & year==`y'
}
}

egen polknow=rowtotal(abserr*), missing
recode VCF0140a (8/9=.a)

gen knowsca=.a
gen knowsca_alt=.a
gen educsca=.a
foreach y of loc levels {
egen knowsca`y'=cut(polknow) if year==`y', group(3)
replace knowsca=knowsca`y' if year==`y'
egen knowsca_alt`y'=cut(polknow) if year==`y', group(4)
replace knowsca_alt=knowsca_alt`y' if year==`y'
_pctile VCF0140a if year==`y', p(60, 99.99)
replace educsca=0 if VCF0140a<=`r(r1)' & year==`y' & !missing(VCF0140a)
replace educsca=1 if VCF0140a>`r(r1)' & VCF0140a<=`r(r2)' & year==`y' & !missing(VCF0140a)
drop knowsca`y' knowsca_alt`y'
}

gen ideoknow=0
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a

by year, sort: egen rescenter=mean(resleft)

by year, sort: egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
by year, sort: egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
by year, sort: egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
by year, sort: egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

by year, sort: egen medianv=median(resleft)
by year, sort: egen resleftvot=sd(resleft) if vote==1
by year, sort: egen resleftabs=sd(resleft) if vote==0
by year, sort: egen resleftsop3=sd(resleft) if knowsca==2
by year, sort: egen resleftsop2=sd(resleft) if knowsca==1
by year, sort: egen resleftsop1=sd(resleft) if knowsca==0
by year, sort: egen resleftpkn=sd(resleft) if ideoknow==1
by year, sort: egen resleftpuk=sd(resleft) if ideoknow==0
by year, sort: egen reslefteducl=sd(resleft) if educsca==0
by year, sort: egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote, by(year)
rename year date
replace date=date*100+11
gen iso=1840
reshape long left, i(date) j(party)
saveold "United States (1972-2016) v1.dta", replace

** European Voter
* Germany (1976, 1983, 1990)
use "The European Voter/ZA3911_EuropeanVoter_stata/ZA3911_de_1961-1998.dta", clear
rename (pchoice lrself) (lastvote resleft)
rename lrp* left#, renumber
recode turnout (1=1) (else=0), gen(vote)
recode lastvote (10/98=.a)
recode year (1976=197610) (1983=198303) (1990=199012) (else=.a), gen(date)

replace left4=.a if date==197610	/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left5=.a if date==197610	/* for the political knowledge scale */
replace left5=.a if date==198303

foreach var of varlist *left*{ 		/* 1-11 scale converted to 0-10 */
replace `var'=`var'-1
}

forval i=1/5 {
by date, sort: egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
}

levelsof date, loc(levels)
foreach y of loc levels {
forval i=1/5 { 
capture noisily {
sum abserr`i' if date==`y'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i') & date==`y'
}
}
}

egen polknow=rowtotal(abserr*), missing
gen knowsca=.a
gen knowsca_alt=.a
gen educsca=.a
foreach y of loc levels {
egen knowsca`y'=cut(polknow) if date==`y', group(3)
replace knowsca=knowsca`y' if date==`y'
egen knowsca_alt`y'=cut(polknow) if date==`y', group(4)
replace knowsca_alt=knowsca_alt`y' if date==`y'
_pctile edu if date==`y', p(60, 99.99)
replace educsca=0 if edu<=`r(r1)' & date==`y' & !missing(edu)
replace educsca=1 if edu>`r(r1)' & edu<=`r(r2)' & date==`y' & !missing(edu)
drop knowsca`y' knowsca_alt`y'
}
tabstat educsca, stats(min max mean) by(date)
replace educsca=1 if edu==2 & date==199012	/* Manual recoding for the 1990 election - cutpoint is .5836 */

tabstat left1 left2, by(date) 				/* CDU/CSU first, SPD second */
gen ideoknow=0
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a

by date, sort: egen rescenter=mean(resleft)

by date, sort: egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
by date, sort: egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
by date, sort: egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
by date, sort: egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

by date, sort: egen medianv=median(resleft)
by date, sort: egen resleftvot=sd(resleft) if vote==1
by date, sort: egen resleftabs=sd(resleft) if vote==0
by date, sort: egen resleftsop3=sd(resleft) if knowsca==2
by date, sort: egen resleftsop2=sd(resleft) if knowsca==1
by date, sort: egen resleftsop1=sd(resleft) if knowsca==0
by date, sort: egen resleftpkn=sd(resleft) if ideoknow==1
by date, sort: egen resleftpuk=sd(resleft) if ideoknow==0
by date, sort: egen reslefteducl=sd(resleft) if educsca==0
by date, sort: egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote, by(date)
gen iso=1276
reshape long left, i(date) j(party)
drop if missing(date)
saveold "Germany (1976, 1983, 1990) v1.dta", replace

* Netherlands (1971-2, 1981-2, 1989)
use "The European Voter/ZA3911_EuropeanVoter_stata/ZA3911_nl_1971-1998.dta", clear
drop if missing(lrp2) & year<=1972 /* Some respondents were not asked the l-r questions */
rename (partycho lrself) (lastvote resleft)
rename lrp* left#, renumber
recode *left* (997/999=.a) (missing=.a)
recode turnout (1=1) (else=0), gen(vote)
recode lastvote (10/999=.a) (missing=.a)
recode year (1971=197104) (1972=197211) (1981=198105) (1982=198209) (1989=198909) (else=.a), gen(date)
recode edu (998/999=.a)

foreach var of varlist *left*{ 		/* 0-6 scale seems to have been converted to 1-10 using the function (1.5*x +1) */
replace `var'=(`var'-1)/1.5 if date<198105 & date!=.a
replace `var'=(10*`var')/6 if date<198105 & date!=.a /* 0-6 scale converted to 0-10 */
}

foreach var of varlist *left*{ 		/* 1-10 scale converted to 0-10 */
replace `var'=(10*`var'-10)/9 if date>=198105 & date!=.a
}

forval i=1/9 {
by date, sort: egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
}

levelsof date, loc(levels)
foreach y of loc levels {
forval i=1/9 {
capture noisily {
sum abserr`i' if date==`y'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i') & date==`y'
}
}
}

egen polknow=rowtotal(abserr*), missing
gen knowsca=.a
gen knowsca_alt=.a
gen educsca=.a
foreach y of loc levels {
capture noisily {
egen knowsca`y'=cut(polknow) if date==`y', group(3)
replace knowsca=knowsca`y' if date==`y'
egen knowsca_alt`y'=cut(polknow) if date==`y', group(4)
replace knowsca_alt=knowsca_alt`y' if date==`y'
_pctile edu if date==`y', p(60, 99.99)
replace educsca=0 if edu<=`r(r1)' & date==`y' & !missing(edu)
replace educsca=1 if edu>`r(r1)' & edu<=`r(r2)' & date==`y' & !missing(edu)
drop knowsca`y' knowsca_alt`y'
}                               
}
tabstat educsca, stats(min max mean) by(date)

tabstat left2 left6 left5, by(date) 	/* PvdA first, KVP second in 1971-2, CDA and PvdA either first or second parties in 1981-2 and 1989 */
gen ideoknow=0
replace ideoknow=1 if left2<left6 & left2!=.a & left6!=.a & date<198105 & date!=.a
replace ideoknow=1 if left2<left5 & left2!=.a & left5!=.a & date>=198105 & date!=.a

by date, sort: egen rescenter=mean(resleft)

by date, sort: egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
by date, sort: egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
by date, sort: egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
by date, sort: egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

by date, sort: egen medianv=median(resleft)
by date, sort: egen resleftvot=sd(resleft) if vote==1
by date, sort: egen resleftabs=sd(resleft) if vote==0
by date, sort: egen resleftsop3=sd(resleft) if knowsca==2
by date, sort: egen resleftsop2=sd(resleft) if knowsca==1
by date, sort: egen resleftsop1=sd(resleft) if knowsca==0
by date, sort: egen resleftpkn=sd(resleft) if ideoknow==1
by date, sort: egen resleftpuk=sd(resleft) if ideoknow==0
by date, sort: egen reslefteducl=sd(resleft) if educsca==0
by date, sort: egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote, by(date)
gen iso=1528
reshape long left, i(date) j(party)
drop if missing(date) | missing(left)
saveold "Netherlands (1971-2, 1981-2, 1989) v1.dta", replace

* Norway (1973-1993)
use "The European Voter/ZA3911_EuropeanVoter_stata/ZA3911_no_1965-1997.dta", clear
keep if year>=1973 & year<=1993
rename (choice lrsp) (lastvote resleft)
rename (lrsoc lrlab lrlib lrchr lrc lrcons lrprog) left(#), addnumber
recode *left* (missing=.a)
recode turnout (2=0), gen(vote)
recode lastvote (2=1) (3=2) (4=3) (5=4) (6=5) (8=6) (9=7) (else=.a)
recode year (1973=197309) (1977=197709) (1981=198109) (1985=198509) (1989=198909) (1993=199309), gen(date)

foreach var of varlist *left*{ 	/* 1-7 scale converted to 0-10 */
replace `var'=(10*`var'-10)/6 if year==1973
}
foreach var of varlist *left*{ 	/* 1-9 scale converted to 0-10 */
replace `var'=(10*`var'-10)/8 if year>=1977 & year<=1981
}
foreach var of varlist *left*{ 	/* 1-10 scale converted to 0-10 */
replace `var'=(10*`var'-10)/9 if year>=1985 & year<=1993
}

forval i=1/7 {
by date, sort: egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
}

levelsof date, loc(levels)
foreach y of loc levels {
forval i=1/7 { 
sum abserr`i' if date==`y'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i') & date==`y'
}
}

egen polknow=rowtotal(abserr*), missing
gen knowsca=.a
gen knowsca_alt=.a
gen educsca=.a
foreach y of loc levels {
egen knowsca`y'=cut(polknow) if date==`y', group(3)
replace knowsca=knowsca`y' if date==`y'
egen knowsca_alt`y'=cut(polknow) if date==`y', group(4)
replace knowsca_alt=knowsca_alt`y' if date==`y'
_pctile edu if date==`y', p(60, 99.99)
replace educsca=0 if edu<=`r(r1)' & date==`y' & !missing(edu)
replace educsca=1 if edu>`r(r1)' & edu<=`r(r2)' & date==`y' & !missing(edu)
drop knowsca`y' knowsca_alt`y'
}
tabstat educsca, stats(min max mean) by(date)

tabstat left1 left6, by(date) 	/* DnA first, H second */
gen ideoknow=0
replace ideoknow=1 if left1<left6 & left1!=.a & left6!=.a

by date, sort: egen rescenter=mean(resleft)

by date, sort: egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
by date, sort: egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
by date, sort: egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
by date, sort: egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

by date, sort: egen medianv=median(resleft)
by date, sort: egen resleftvot=sd(resleft) if vote==1
by date, sort: egen resleftabs=sd(resleft) if vote==0
by date, sort: egen resleftsop3=sd(resleft) if knowsca==2
by date, sort: egen resleftsop2=sd(resleft) if knowsca==1
by date, sort: egen resleftsop1=sd(resleft) if knowsca==0
by date, sort: egen resleftpkn=sd(resleft) if ideoknow==1
by date, sort: egen resleftpuk=sd(resleft) if ideoknow==0
by date, sort: egen reslefteducl=sd(resleft) if educsca==0
by date, sort: egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote, by(date)
gen iso=1578
reshape long left, i(date) j(party)
saveold "Norway (1977-1993) v1.dta", replace

* Sweden (1979-1994)
use "The European Voter/ZA3911_EuropeanVoter_stata/ZA3911_se_1956-1998.dta", clear
keep if year>=1979 & year<=1994
rename (party lrpown) (lastvote resleft)
rename lrp* left#, renumber
recode *left* (98/999=.a) (missing=.a)
recode vote (99=0)
recode lastvote (10=6) (11=7) (12=8) (15/99=.a)
rename year date
replace date=date*100+09
recode edu (8/99=.a)

replace left6=.a if date==198209	/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left7=.a if date==198209	/* for the political knowledge scale */
replace left7=.a if date==198509	/* Placement questions were not asked for 6 other parties */
replace left6=.a if date==198809
replace left8=.a if date==199409

forval i=1/8 {
capture noisily { 
by date, sort: egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i')
}
}

levelsof date, loc(levels)
foreach y of loc levels {
forval i=1/8 {
capture noisily { 
sum abserr`i' if date==`y'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i') & date==`y'
}
}
}

egen polknow=rowtotal(abserr*), missing
gen knowsca=.a
gen knowsca_alt=.a
gen educsca=.a
foreach y of loc levels {
egen knowsca`y'=cut(polknow) if date==`y', group(3)
replace knowsca=knowsca`y' if date==`y'
egen knowsca_alt`y'=cut(polknow) if date==`y', group(4)
replace knowsca_alt=knowsca_alt`y' if date==`y'
_pctile edu if date==`y', p(60, 99.99)
replace educsca=0 if edu<=`r(r1)' & date==`y' & !missing(edu)
replace educsca=1 if edu>`r(r1)' & edu<=`r(r2)' & date==`y' & !missing(edu)
drop knowsca`y' knowsca_alt`y'
}
tabstat educsca, stats(min max mean) by(date)

tabstat left2 left5, by(date) 		/* SAP first, MSP second */
gen ideoknow=0
replace ideoknow=1 if left2<left5 & left2!=.a & left5!=.a

by date, sort: egen rescenter=mean(resleft)

by date, sort: egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
by date, sort: egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
by date, sort: egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
by date, sort: egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

by date, sort: egen medianv=median(resleft)
by date, sort: egen resleftvot=sd(resleft) if vote==1
by date, sort: egen resleftabs=sd(resleft) if vote==0
by date, sort: egen resleftsop3=sd(resleft) if knowsca==2
by date, sort: egen resleftsop2=sd(resleft) if knowsca==1
by date, sort: egen resleftsop1=sd(resleft) if knowsca==0
by date, sort: egen resleftpkn=sd(resleft) if ideoknow==1
by date, sort: egen resleftpuk=sd(resleft) if ideoknow==0
by date, sort: egen reslefteducl=sd(resleft) if educsca==0
by date, sort: egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* vote, by(date)
gen iso=1752
reshape long left, i(date) j(party)
drop if missing(date)
saveold "Sweden (1979-1994) v1.dta", replace

*****
import excel "/Users/mmoral/Dropbox/Sabancı University/-ONGOING/On the Relationship between Party Polarization and Citizen Polarization/Parties NES v10.xlsx", sheet("Stata Corrected") firstrow case(lower) clear
saveold "Stata Corrected_NES.dta", replace

use "Australia (1998) v1.dta", clear
append using "Australia (2001) v1.dta" "Australia (2010) v1.dta" "Australia (2016) v1.dta" "Austria (2006) v1.dta" "Denmark (2005) v1.dta" "Denmark (2015) v1.dta" "Finland (1991) v1.dta" "Germany (1976, 1983, 1990) v1.dta" ///
"Greece (2012) v1.dta" "Netherlands (1971-2, 1981-2, 1989) v1.dta" "Netherlands (2017) v1.dta" "New Zealand (1990) v1.dta" "New Zealand (1993) v1.dta" "New Zealand (1999) v1.dta" "New Zealand (2005) v1.dta" ///
"Norway (1977-1993) v1.dta" "Spain (1989) v1.dta" "Spain (2015) v1.dta" "Spain (2016) v1.dta" "Spain (2019) v1.dta" "Sweden (1979-1994) v1.dta" "Switzerland (1995) v1.dta" "Switzerland (2015) v1.dta" "United Kingdom (1983) v1.dta" "United Kingdom (2017) v1.dta" "United States (1972-2016) v1.dta"
saveold "nes_merged v1.dta", replace

rename party pid
merge 1:1 pid iso date using "Stata Corrected_NES.dta"
drop if missing(country)			/* 6 parties from Sweden (Sweden 1979-1988) - w/ no LR placement */
sort iso date pid
tab pid iso if missing(partyname) 	/* 46 unmatched, non-legislative parties w/ no vote share in the CMP data (Spain 2019 (8) to be checked from CMP2020a!) */
rename country countryname
drop study _merge
gen str study="NES"
rename vote turnout_sample
order study countryname iso date pid party partyname resleft resleftvot resleftabs resleftsop3 resleftsop2 resleftsop1 resleftpkn resleftpuk resleftsop_alt* rescenter medianv left, first
saveold "nes_merged v2.dta", replace
